package com.mm.controller.web.taxi;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.mm.base.annotation.VerifyToken;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import com.mm.domain.taxi.DTaxiAddressInfo;
import com.mm.serivce.taxi.IDTaxiAddressInfoService;
import com.mm.vo.web.user.DownTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 用户打车地址表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Api(value = "/ta/v1", tags = {"用户打车地址"})
@Slf4j
@VerifyToken
@RestController
@RequestMapping("/ta/v1")
@AopLog(type = "用户打车地址", stackTraceOnErr = true)
public class DTaxiAddressInfoController {

    private final IDTaxiAddressInfoService addressInfoService;

    public DTaxiAddressInfoController(IDTaxiAddressInfoService addressInfoService) {
        this.addressInfoService = addressInfoService;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "page", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "limit", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "addressForm", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "addressLast", value = "")
    })
    @ApiOperation(value = "打车地址查询接口", notes = "", httpMethod = "POST")
    @PostMapping("/queryAddressPage")
    public DResult queryAddressPage(@RequestParam int page, @RequestParam int limit, @RequestParam(required = false) String addressForm, @RequestParam(required = false) String addressLast){
        Page<DTaxiAddressInfo> levelInfoPage = addressInfoService.queryAddressPage(page, limit, addressForm, addressLast);
        DPage<DTaxiAddressInfo> levelInfoDPage = DPage.to(levelInfoPage);
        return DResult.of(levelInfoDPage);
    }

    @ApiOperation(value = "打车地址下拉列表查询接口", notes = "", httpMethod = "GET")
    @GetMapping("/queryTaxiAddress")
    public DResult queryTaxiAddress(){
        List<DTaxiAddressInfo> userList = addressInfoService.queryTaxiAddressAll();
        List<DownTreeVo> downTreeVos = new ArrayList<>();
        Optional.ofNullable(userList).orElse(new ArrayList<>())
                .forEach(a -> {
                    downTreeVos.add(DownTreeVo.builder().id(a.getTaxiId())
                            .title(a.getTaxiFirst() + "-" + a.getTaxiLast()).build());
                });
        return DResult.of(downTreeVos);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "taxiId", value = "")
    })
    @ApiOperation(value = "打车地址查询单个接口", notes = "", httpMethod = "POST")
    @PostMapping("/queryAddressOne")
    public DResult queryAddressOne(@RequestParam Integer taxiId){
        return DResult.of(addressInfoService.queryAddressOne(taxiId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DTaxiAddressInfo", dataTypeClass = DTaxiAddressInfo.class, name = "addressInfo", value = "")
    })
    @ApiOperation(value = "打车地址编辑接口", notes = "", httpMethod = "POST")
    @Transactional
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DTaxiAddressInfo addressInfo) {
        boolean flag = false;
        if (addressInfo.getTaxiId() < 0) {
            // 新增
            flag = addressInfoService.save(addressInfo);
        } else if (addressInfo.getTaxiId()  > 0) {
            // 更新
            flag = addressInfoService.updateById(addressInfo);
        }
        return DResult.of(flag);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "taxiId", value = "")
    })
    @ApiOperation(value = "打车地址删除接口", notes = "", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer taxiId){
        boolean flag = false;
        if (taxiId > 0) {
            flag = addressInfoService.removeAddress(taxiId);
        }
        return DResult.of(flag);
    }

}
